﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


using System.Data.SqlClient;public partial class Admin_QuanLyKhachHang_DanhSachKhachHang : System.Web.UI.Page
{
    private DataAccess da = new DataAccess();
    private string strMA = string.Empty;
    private string strTEN = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminID"] == null)
        {
            Response.Write("<script language='javascript'>alert('Bạn phải đăng nhập / You must login');location.href='/../Admin/Login.aspx';</script>");
        }
        if (!IsPostBack)
        {
            this.GetKhachHangList();
        }
    }
    protected void KhachHang_AddNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("ThemMoiKhachHang.aspx");
    }
    protected void NhomHang_Delete_Click(object sender, EventArgs e)
    {
        this.DeleteKhachHang();
    }
    protected void display_Click(object sender, EventArgs e)
    {
        this.GetKhachHangList();
    }
    protected void NVMASearch_Click(object sender, EventArgs e)
    {
        this.strMA = this.txtMA.Text;
        this.strTEN = this.txtTen.Text;
        this.GetKhachHangList();
    }
    protected void GetKhachHangList()
    {

        DataSet dts = new DataSet();
        SqlDataAdapter adt = new SqlDataAdapter();
        SqlCommand comm = new SqlCommand("GetKH", da.SqlConn());
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.AddWithValue("@MA", strMA);
        comm.Parameters.AddWithValue("@TEN", strTEN);
        comm.Connection.Open();
        adt.SelectCommand = comm;
        adt.Fill(dts);
        this.GV_NVList.DataSource = dts;
        this.GV_NVList.DataBind();
        dts.Clear();
        adt.Dispose();
        comm.Connection.Close();
        comm.Connection.Dispose();
    }
    protected void DeleteKhachHang()
    {
        foreach (string item in HttpContext.Current.Request.Form)
        {
            if (item.Substring(0, 9) == "chbDelete")
            {

                string strMa = item.Substring(9, item.Length - 9);
                //Response.Write("<script language='javascript'>alert('" + strMa + "')</script>");
                SqlCommand comm = new SqlCommand("deleteKH", da.SqlConn());
                comm.CommandType = CommandType.StoredProcedure;
                comm.Parameters.AddWithValue("@Ma", strMa);
                try
                {
                    comm.Connection.Open();
                    comm.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message.ToString());
                }
                finally
                {
                    comm.Connection.Close();
                    comm.Connection.Dispose();
                }
                this.GetKhachHangList();
            }
        }
    }
    protected void GV_NVList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GV_NVList.PageIndex = e.NewPageIndex;
        this.GetKhachHangList();
    }
}
